package com.supermap.dialogs;

import com.supermap.data.*;
import com.supermap.dialogs.advance.CJDialog;
import com.supermap.mapping.ThemeGraph;
import com.supermap.mapping.ThemeGraphItem;
import com.supermap.mapping.ThemeGraphType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ThemeGraphDialog extends CJDialog {
    private JComboBox<String> firstValueComboBox;
    private JComboBox<String> secondValueComboBox;

    private DatasetVector datasetVector;
    private ThemeGraph themeGraph;
    private Color firstColor;
    private Color secondColor;

    public ThemeGraphDialog(Frame owner, String title, boolean modal, DatasetVector datasetVector) {
        super(owner, title, modal, 260, 165, new Object[]{datasetVector});
    }

    public ThemeGraph getThemeGraph() {
        return themeGraph;
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.datasetVector = (DatasetVector) args[0];
    }

    @Override
    protected void initializeCore() {
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosed(e);
                themeGraph = null;
            }
        });
        super.confirmBtn.setText("创建");

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(4, 1));
        add(mainPanel, BorderLayout.CENTER);

        // 下拉框
        FieldInfos fieldInfos = datasetVector.getFieldInfos();
        firstValueComboBox = new JComboBox<>();
        for (int i = 0; i < fieldInfos.getCount(); i++) {
            FieldInfo fieldInfo = fieldInfos.get(i);
            FieldType fieldType = fieldInfo.getType();
            if (fieldType != FieldType.LONGBINARY && fieldType != FieldType.JSONB) {
                firstValueComboBox.addItem(fieldInfo.getCaption());
            }
        }
        mainPanel.add(firstValueComboBox);

        // 颜色设置
        JButton firstColorButton = new JButton();
        firstColorButton.setText("统计颜色");
        firstColor = Color.RED;
        firstColorButton.setBackground(firstColor);
        firstColorButton.addActionListener(e -> {
            firstColor = JColorChooser.showDialog(this, "统计颜色", firstColor);
            firstColorButton.setBackground(firstColor);
        });
        mainPanel.add(firstColorButton);

        secondValueComboBox = new JComboBox<>();
        for (int i = 0; i < fieldInfos.getCount(); i++) {
            FieldInfo fieldInfo = fieldInfos.get(i);
            FieldType fieldType = fieldInfo.getType();
            if (fieldType != FieldType.LONGBINARY && fieldType != FieldType.JSONB) {
                secondValueComboBox.addItem(fieldInfo.getCaption());
            }
        }
        mainPanel.add(secondValueComboBox);

        // 颜色设置
        JButton secondColorButton = new JButton();
        secondColorButton.setText("统计颜色");
        secondColor = Color.GREEN;
        secondColorButton.setBackground(secondColor);
        secondColorButton.addActionListener(e -> {
            secondColor = JColorChooser.showDialog(this, "统计颜色", secondColor);
            secondColorButton.setBackground(secondColor);
        });
        mainPanel.add(secondColorButton);
    }

    @Override
    protected void doConfirmCore() {
        setVisible(false);
        themeGraph = new ThemeGraph();
        themeGraph.setGraphType(ThemeGraphType.LINE);
        ThemeGraphItem firstThemeGraphItem = new ThemeGraphItem();
        firstThemeGraphItem.setGraphExpression((String) firstValueComboBox.getSelectedItem());
        GeoStyle firstGeoStyle = new GeoStyle();
        firstGeoStyle.setFillBackColor(Color.BLACK);
        firstGeoStyle.setFillForeColor(firstColor);
        firstGeoStyle.setFillGradientMode(FillGradientMode.LINEAR);
        firstThemeGraphItem.setUniformStyle(firstGeoStyle);
        themeGraph.add(firstThemeGraphItem);
        ThemeGraphItem secondThemeGraphItem = new ThemeGraphItem();
        secondThemeGraphItem.setGraphExpression((String) secondValueComboBox.getSelectedItem());
        GeoStyle secondGeoStyle = new GeoStyle();
        secondGeoStyle.setFillBackColor(Color.BLACK);
        secondGeoStyle.setFillForeColor(secondColor);
        secondGeoStyle.setFillGradientMode(FillGradientMode.LINEAR);
        secondThemeGraphItem.setUniformStyle(secondGeoStyle);
        themeGraph.add(secondThemeGraphItem);
    }
}
